<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class ShopOrderModel extends AbstractModel
{
    protected $tableName = 't_shop_order';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws Exception|Throwable
     */
    public function getOrder($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addOrder($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('商城-订单-内容');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colBigInt('uuid', 20)->setDefaultValue(0)->setIsNotNull()->setColumnComment('下单用户编号');
        $table->colInt('aid', 20)->setDefaultValue(0)->setIsNotNull()->setColumnComment('收货地址d');
        $table->colBigInt('puid1', 20)->setDefaultValue(0)->setIsNotNull()->setColumnComment('推荐一层用户');
        $table->colBigInt('puid2', 20)->setDefaultValue(0)->setIsNotNull()->setColumnComment('推荐二层用户');
        $table->colVarChar('order_no', 20)->setDefaultValue('')->setColumnComment('商品订单单号');
        $table->colDecimal('amount_real', 20,2)->setDefaultValue(0.00)->setColumnComment('订单实际金额');
        $table->colDecimal('amount_total', 20,2)->setDefaultValue(0.00)->setColumnComment('订单统计金额');
        $table->colDecimal('amount_goods', 20,2)->setDefaultValue(0.00)->setColumnComment('商品统计金额');
        $table->colDecimal('amount_reduct', 20,2)->setDefaultValue(0.00)->setColumnComment('随机减免金额');
        $table->colDecimal('amount_express', 20,2)->setDefaultValue(0.00)->setColumnComment('快递费用金额');
        $table->colDecimal('amount_discount', 20,2)->setDefaultValue(0.00)->setColumnComment('折扣后的金额');
        $table->colVarChar('payment_type', 20)->setDefaultValue('')->setColumnComment('实际支付平台');
        $table->colVarChar('payment_code', 20)->setDefaultValue('')->setColumnComment('实际通道编号');
        $table->colVarChar('payment_allow', 999)->setDefaultValue('')->setColumnComment('允许支付通道');
        $table->colVarChar('payment_trade', 80)->setDefaultValue('')->setColumnComment('实际支付单号');
        $table->colTinyInt('payment_status',1)->setDefaultValue(0)->setColumnComment('实际支付状态');
        $table->colVarChar('payment_image', 999)->setDefaultValue('')->setColumnComment('支付凭证图片');
        $table->colDecimal('payment_amount', 20,2)->setDefaultValue(0.00)->setColumnComment('实际支付金额');
        $table->colDecimal('payment_balance', 20,2)->setDefaultValue(0.00)->setColumnComment('余额抵扣金额');
        $table->colVarChar('payment_remark', 500)->setDefaultValue('')->setColumnComment('支付结果描述');
        $table->colVarChar('payment_datetime', 20)->setDefaultValue('')->setColumnComment('支付到账时间');
        $table->colBigInt('number_goods', 20)->setDefaultValue(0)->setIsNotNull()->setColumnComment('订单商品数量');
        $table->colBigInt('number_express', 20)->setDefaultValue(0)->setIsNotNull()->setColumnComment('订单快递计数');
        $table->colTinyInt('truck_type',1)->setDefaultValue(0)->setColumnComment('物流配送类型(0无需配送,1需要配送)');
        $table->colDecimal('rebate_amount', 20,2)->setDefaultValue(0.00)->setColumnComment('参与返利金额');
        $table->colDecimal('reward_balance', 20,2)->setDefaultValue(0.00)->setColumnComment('奖励账户余额');
        $table->colVarChar('order_remark', 999)->setDefaultValue('')->setColumnComment('订单用户备注');
        $table->colTinyInt('cancel_status',1)->setDefaultValue(0)->setColumnComment('订单取消状态');
        $table->colVarChar('cancel_remark', 200)->setDefaultValue('')->setColumnComment('订单取消描述');
        $table->colVarChar('cancel_datetime', 20)->setDefaultValue('')->setColumnComment('订单取消时间');
        $table->colTinyInt('deleted_status',1)->setDefaultValue(0)->setColumnComment('订单删除状态(0未删,1已删)');
        $table->colVarChar('deleted_remark', 255)->setDefaultValue('')->setColumnComment('订单删除描述');
        $table->colVarChar('deleted_datetime', 20)->setDefaultValue('')->setColumnComment('订单删除时间');
        $table->colTinyInt('status',1)->setDefaultValue(1)->setColumnComment('订单流程状态(0已取消,1预订单,2待支付,3支付中,4已支付,5已发货,6已完成)');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('idx_shop_order_status', 'status');
        $table->indexNormal('idx_shop_order_orderno', 'order_no');
        $table->indexNormal('idx_shop_order_cancel_status', 'cancel_status');
        $table->indexNormal('idx_shop_order_payment_status', 'payment_status');
        $table->indexNormal('idx_shop_order_from', 'puid1');
        $table->indexNormal('idx_shop_order_deleted', 'deleted_status');
        $table->indexNormal('idx_shop_order_mid', 'uuid');
        return $table;
    }
}